$(function(){
	loadHtml();
	
	//二手房
	$(".house_select_filter_item").click(function(){
		$(this).addClass("house_select_filter_item_on").siblings().removeClass("house_select_filter_item_on");
	});
	$(".house_show_title").click(function(){
		$(this).addClass("house_show_title_on").siblings().removeClass("house_show_title_on");
	});
	
	//二手房详情
	$(".collect").click(function(){
		$(this).toggleClass("collect_click");
	});
	
	//家电数码详情-宝贝详情
	$('.degital_addr_select').click(function(){
		$(this).toggleClass('degital_addr_select_bg').children(".degital_addr_select_detail").toggle().find('.degital_addr_select_item').click(function(){
			$(this).parents().prev(".degital_addr_selected").html($(this).html());
		});
	});
	
	$('.qq_contact').mouseover(function(){
		$(this).children('img').attr("src","../style/img/qq_maijia_on.png");
	}).mouseout(function(){
		$(this).children('img').attr("src","../style/img/qq_maijia.png");
	});
	
	$('.degital_conduct_title').eq(0).click(function(){
		$(this).addClass("degital_conduct_title_on").siblings().removeClass("degital_conduct_title_on").parent().next().children('.degital_conduct_detail').show().siblings(".message_div").hide();
	});
	$('.degital_conduct_title').eq(1).click(function(){
		$(this).addClass("degital_conduct_title_on").siblings().removeClass("degital_conduct_title_on").parent().next().children('.degital_conduct_detail').hide().siblings(".message_div").show();
	});
	
	//留言
	$(".message_btn").click(function(){
		var cont = $(this).parents().prev(".message_textarea").val();
		$(".message_cont").html(cont);
		$(this).parents(".message_wrap").add(".message_detail").show();
	});
	var m = 0;
	var n = m+1;
	$(".message_reply_btn").click(function(){
		$(".reply_div").toggle().find(".reply_btn").click(function(){
			$(this).parents(".reply_div").hide();
			m++;
			var replyText = $('.reply_textarea').val();
			var add_reply = "<div class='reply_cont' id='rebox"+n+"'>"+
								"<span class='reply_user_head fl'>"+
								"<img class='img_style' src='../style/img/head.png'/>"+
							"</span>"+
							"<div class='reply_user_info fr'>"+
								"<p class='reply_user_cont'>"+
									"<span class='message_user_name'><time class='reply_source'>妈咪宝贝</time>&nbsp;&nbsp;回复@<time class='reply_to'>宝宝＊＊小九</time>：</span>"+
									"<span class='reply_cont_show'>"+replyText+"</span>"+
								"</p>"+
							"<p class='mt10'>"+
								"<time class='reply_user_time fl'>2016-06-12 15:30:17</time>"+
								"<span class='fr message_btn_wrap'>"+
									"<span class='message_btn_hover message_reply_btn'>回复</span>"+
									"<span class='message_btn_hover reply_delete_btn'>删除</span>"+
								"</span>"+
							"</p>"+
							"</div>"+
							"</div>"	
			$("#"+"rebox"+n).prepend(add_reply);
		
		});
		
	});


	//购物车JS
	
	function qxHash(){
		//点击全选时统计选中的商品个数
		var qxNum = $(".addshoppingcar_item[data_state=false]").length;
		if(qxNum > 0){
			$(".shop_selectall").removeClass("addshoppingcar_item_on");
		}else if(qxNum <= 0){
			$(".shop_selectall").addClass("addshoppingcar_item_on");
		}
	}
	
	//点击卖家下的单个商品
	function dxGoods(){
		$(".shop_goods_left").click(function(){
			if($(this).hasClass("addshoppingcar_item_on")){
				$(this).removeClass("addshoppingcar_item_on").parent(".addshoppingcar_item").attr("data_state","false").parent().prev().children(".addshoppingcar_sale_title").removeClass("addshoppingcar_item_on")
				.parents(".addshoppingcar_div").attr("data_sale","false");
				
				totalPrice();
			}else{
				$(this).addClass("addshoppingcar_item_on").parent(".addshoppingcar_item").attr("data_state","true").parent().prev().children(".addshoppingcar_sale_title").addClass("addshoppingcar_item_on")
				.parents(".addshoppingcar_div").attr("data_sale","true");

				totalPrice();
			}
			xzGoodsNum($(this));
			
			qxHash();
			
		});
	}
	dxGoods();
	
	function xzGoodsNum(obj2){
		//获取选中的元素个数
			var shopcartotalnum = obj2.parents(".addshoppingcar_detail").children(".addshoppingcar_item").length;
			var shopcarNum = obj2.parents(".addshoppingcar_detail").children(".addshoppingcar_item[data_state=true]").length;
			totalNum();
			if(shopcarNum > 0 && shopcarNum != shopcartotalnum){
				obj2.parents(".addshoppingcar_detail").prev().children(".addshoppingcar_sale_title").addClass("addshoppingcar_item_on").parents(".addshoppingcar_div").attr({"data_sale":"true","data_all":"false"});
			}else if(shopcarNum == 0){
				obj2.parents(".addshoppingcar_detail").prev().children(".addshoppingcar_sale_title").removeClass("addshoppingcar_item_on").parents(".addshoppingcar_div").attr({"data_sale":"false","data_all":"false"});
			}else if(shopcarNum == shopcartotalnum){
				obj2.parents(".addshoppingcar_div").attr("data_all","true");
			}
	}
	
	//点击卖家
	function Sale(){
		$(".addshoppingcar_sale_title").click(function(){
			if($(this).hasClass("addshoppingcar_item_on")){
				$(this).removeClass("addshoppingcar_item_on").parents(".addshoppingcar_div").attr({"data_sale":"false","data_all":"false"}).children().children(".addshoppingcar_item").attr("data_state","false")
				.children(".shop_goods_left").removeClass("addshoppingcar_item_on");
				
				totalPrice();
			}else{
				$(this).addClass("addshoppingcar_item_on").parents(".addshoppingcar_div").attr({"data_sale":"true","data_all":"true"}).children().children(".addshoppingcar_item").attr("data_state","true")
				.children(".shop_goods_left").addClass("addshoppingcar_item_on");
				
				totalPrice();
			}
			
			qxHash();
			
			totalNum();
			
		});
	}
	Sale();
	
	
	
	//点击全选
	$(".shop_selectall").click(function(){
		if($(this).hasClass("addshoppingcar_item_on")){
			$(this).removeClass("addshoppingcar_item_on").parents(".addshoppingcar_cont").next(".addshoppingcar_div_wrap").children(".addshoppingcar_div").attr({"data_sale":"false","data_all":"false"}).children().children(".addshoppingcar_sale_title")
			.removeClass("addshoppingcar_item_on").parent().next().children(".addshoppingcar_item").attr("data_state","false").children(".shop_goods_left").removeClass("addshoppingcar_item_on");
		
			totalPrice();
		}else{
			$(this).addClass("addshoppingcar_item_on").parents(".addshoppingcar_cont").next(".addshoppingcar_div_wrap").children(".addshoppingcar_div").attr({"data_sale":"true","data_all":"true"}).children().children(".addshoppingcar_sale_title")
			.addClass("addshoppingcar_item_on").parent().next().children(".addshoppingcar_item").attr("data_state","true").children(".shop_goods_left").addClass("addshoppingcar_item_on");
		
			totalPrice();
		}
		totalNum();

	});
	
	
	//删除单独的商品
	
	function deleteOne(){
		$(".addshoppingcar_delete").click(function(){
			
			var $clickItem = $(this).parents(".addshoppingcar_item").children(".shop_goods_left");
			//弹出提示框
			if($clickItem.hasClass("addshoppingcar_item_on")){
				$clickItem.addClass("addshoppingcar_item_on").parent(".addshoppingcar_item").attr("data_state","true").parents(".addshoppingcar_div").attr("data_sale","true").children()
				.children(".addshoppingcar_sale_title").addClass("addshoppingcar_item_on");
				totalNum();
				
				fadeShow();

				var fadeArray = [".deleteornot_btn",".deleteornot_close"];
				for(j = 0; j < fadeArray.length; j++){
					fadeRmove(fadeArray[j]);
					$(fadeArray[0]).click(function(){
						if($(this).html() == "确定"){
						//获取选中的元素个数
							var shopcartotalnum = $(".addshoppingcar_div[data_sale=true]").children(".addshoppingcar_detail").children(".addshoppingcar_item").length;
							var shopcarNum = $(".addshoppingcar_item[data_state=true]").length;
							if(shopcarNum == shopcartotalnum){
								$(".addshoppingcar_item[data_state=true]").parents(".addshoppingcar_div").attr("data_all","true");
							}
							//判断同类元素是否全部删除完
							$(".addshoppingcar_div[data_all=true]").remove();
							$(".addshoppingcar_item[data_state=true]").remove();
							$(".shopcar_num").html(shopcarNum);
						}
						totalPrice();
					});
				}
			}
			
		});
	}
	deleteOne();
	

	//删除购物车选择的商品
	$(".shopselected_delete").click(function(){
		fadeShow();
		var fadeArray = [".deleteornot_btn",".deleteornot_close"];
		for(j = 0; j < fadeArray.length; j++){
			fadeRmove(fadeArray[j]);
			$(fadeArray[0]).click(function(){
				if($(this).html() == "确定"){
					$(".addshoppingcar_item[data_state=true]").remove();
					$(".addshoppingcar_div[data_all=true]").remove();
//					dxGoods();
					totalNum();
				}
				totalPrice();
			});
		}
		
	});
	
	//显示遮罩层
	function fadeShow(){
		//修改
		$("#fade").show().height($("body").outerHeight(true));
		$(".deleteornot").show();
		//修改
		if($(".addshoppingcar_item[data_state=true]").length <= 0){
			$(".deleteornot_tips").html("对不起！您还没有选中任何商品");
		}else{
			$(".deleteornot_tips").html("您确定要删除选中的商品吗？");
		}
	}
	
	
	//隐藏遮罩层
	//修改
	function fadeRmove(obj){
		$(obj).click(function(){
			$(this).parents(".deleteornot").hide();
			$("#fade").hide();
		});
	}
	//修改
	
		//统计总共选中的商品个数
	function totalNum(){
		var shopcarNum = $(".addshoppingcar_item[data_state=true]").length;
		$(".shopcar_num").html(shopcarNum);	
	}
	
//	//计算商品总价格
	function totalPrice(){
		var total = 0;
		var goodsNum = $(".addshoppingcar_item");
		for(var k = 0; k < goodsNum.length; k++){
			
			if($(goodsNum[k]).attr("data_state") == "true"){
				var onePrice = $(goodsNum[k]).attr("data_state","true").children().children().children(".addshoppingcar_price").children(".one_price");
				total= total + parseFloat(onePrice.html());
			}
			$(".shoppingcar_total").html(total);
		}
		
		if($(".addshoppingcar_item[data_state=true]").length == 0){
			$(".shoppingcar_total").html(0);
		}
	}
	
	//今日特卖
	function Show(index){
		$(".sale_cont").eq(index).show().siblings().hide();
	}
	Show(0);
	$(".banner_ul .banner_item").click(function(){
		var saleIndex = $(this).index();
		Show(saleIndex);
	});


	//修改  增加
	$(".banner_li").click(function(){
		$(this).css({"background":"#FB7A2D","color":"#fff"}).parent().siblings().children(".banner_li").css({"background":"#fff","color":"#FB7A2D"});
	});
	//修改  增加
	
})
function loadHtml(){
	$('#header_div').load('header.html');
	$('#footer_div').load('footer.html');
	$("#page_div").load("pagination.html");
}




